<?php
namespace Admin\Controller;
class RoleController extends AdminBaseController
{
    /***
     * @privilege Admin:角色管理->添加:82852027496
     */
    public function add()
    {
        if (IS_POST) {
            $data = I('post.data');
            $this->_addData('admin_role', $data);
        } else {
            $this->_display('edit', '角色管理增加');
        }
    }

    /***
     * @privilege Admin:角色管理->编辑:33288641114
     */
    public function edit()
    {
        if (IS_POST) {
            $data = I('post.data');
            $this->_editData('admin_role', $data);
        } else {
            $info = $this->_detail('admin_role');
            $this->assign([
                'info' => $info
            ]);
            $this->_display('edit', '角色管理编辑');
        }
    }

    /***
     * @privilege Admin:角色管理->列表:50303070432
     */
    public function lis()
    {
        $where = 'status = 1';
        $result = $this->_list('admin_role', $where, 10);
        $this->assign($result);
        $this->_display('lis', '角色管理列表');
    }

    /***
     * @privilege Admin:角色管理->删除:85440388719
     */
    public function del()
    {

        $this->_delAll('admin_role');
    }
    /***
     * @privilege Admin:角色管理->权限列表:40170133259
     */
    public function power()
    {
        $roleId = I('get.id');
        $ruleModel = D('Rule');
        $allRule = $ruleModel ->getAllRule();
        $ownRule = $ruleModel ->getOwnRule($roleId);
        $this->assign([
            'allRule' =>$allRule,
            'ownRule' =>$ownRule,
            'roleId' =>$roleId
        ]);
        $this->_display('power','权限列表');
    }
    /***
     * @privilege Admin:角色管理->修改权限:18004150984
     */
    public function savePower()
    {
        if(IS_POST){
            $powerData = I('post.power','');
            $roleId = I('post.roleId','');
            $model = M('admin_role_rule');
            $model ->startTrans();
            $model->where("role_id = $roleId")->delete();
            foreach($powerData as $k =>$v){
                $data['rule_id'] = $v;
                $data['role_id'] = $roleId;
                $rst = $model->add($data);
                if($rst === false){
                    $model->rollback();
                    $this->message($model->getDbError(),0);
                }
            }
            $model->commit();
            $this->message('操作成功',1);
        }else{
            exit('无权操作!');
        }
    }
}